package edu.nf.project.services.storeOperation.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import edu.nf.project.entitys.dto.storeOperation.WarehouseDTO;
import edu.nf.project.entitys.entity.storeOperation.convention.cargo.Cargo;
import edu.nf.project.entitys.entity.storeOperation.convention.cargo.Warehouse;
import edu.nf.project.mappers.storeOperation.convention.CargoMapper;
import edu.nf.project.mappers.storeOperation.convention.WarehouseMapper;
import edu.nf.project.services.storeOperation.WarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class WarehouseServiceImpl implements WarehouseService {
    @Autowired
    private WarehouseMapper warehouseMapper;

    @Autowired
    private CargoMapper cargoMapper;

    @Override
    public List<WarehouseDTO> getAll() {
        // 1. 获取所有仓库信息
        List<Warehouse> warehouses = warehouseMapper.selectList(null);
        List<WarehouseDTO> warehouseDTOs = new ArrayList<>();
        
        // 2. 遍历仓库列表，为每个仓库创建DTO对象
        for (Warehouse warehouse : warehouses) {
            WarehouseDTO dto = new WarehouseDTO();
            dto.setWarehouse(warehouse);
            
            // 根据cargoId查询货物信息
            Cargo cargo = cargoMapper.selectById(warehouse.getCargoId());
            if (cargo != null) {
                dto.setCargo(cargo);
            }
            
            warehouseDTOs.add(dto);
        }
        
        return warehouseDTOs;
    }

    @Override
    @Transactional
    public Boolean inventoryPlus(Integer cargoId, Integer quantity) {
        // 1. 参数校验
        if (quantity == null || quantity <= 0) {
            return false;
        }
        
        // 2. 构建更新条件
        LambdaUpdateWrapper<Warehouse> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Warehouse::getCargoId, cargoId)
                    .setSql("inventory = inventory + " + quantity);
        
        // 3. 执行更新操作
        return warehouseMapper.update(null, updateWrapper) > 0;
    }

    @Override
    @Transactional
    public Boolean inventoryMinus(Integer cargoId, Integer quantity) {
        // 1. 参数校验
        if (quantity == null || quantity <= 0) {
            return false;
        }
        
        // 2. 先查询当前库存
        LambdaQueryWrapper<Warehouse> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Warehouse::getCargoId, cargoId);
        Warehouse warehouse = warehouseMapper.selectOne(queryWrapper);
        
        // 3. 检查库存是否足够
        if (warehouse == null || warehouse.getInventory() < quantity) {
            return false;
        }
        
        // 4. 构建更新条件
        LambdaUpdateWrapper<Warehouse> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Warehouse::getCargoId, cargoId)
                    .setSql("inventory = inventory - " + quantity);
        
        // 5. 执行更新操作
        return warehouseMapper.update(null, updateWrapper) > 0;
    }
}
